#include "cclient.h"
#include "cbuffer.h"
#include <iostream>
using namespace std;

CClient::CClient(int sockfd, int send_buffer_size, int recv_buffer_size){
    m_send_buffer = new CBuffer(send_buffer_size);
    m_recv_buffer = new CBuffer(recv_buffer_size);
    m_sockfd = sockfd;
}

CClient::~CClient(){
    if(m_send_buffer){
        delete m_send_buffer;
        m_send_buffer = nullptr;
    }
    if(m_recv_buffer){
        delete m_recv_buffer;
        m_recv_buffer = nullptr;
    }
    if(m_sockfd > 0){
        close(m_sockfd);
        m_sockfd = -1;
    }
}

int CClient::recvdata(){
    return m_recv_buffer->operator<<(m_sockfd);
}

int CClient::senddata(){
    return m_send_buffer->operator>>(m_sockfd);
}

int CClient::senddata(CBuffer * buffer){
    return m_send_buffer->push(buffer->c_str(), buffer->size());
}

int CClient::senddata(const char * data, int size){
    return m_send_buffer->push(data, size);
}
